<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="close()"
    :destroy-on-close="true"
    @opened="opened()"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'"
      :disabled="disabled"
    >
      <!-- <el-form-item label="ID" prop="id">
              <el-input v-model="dataForm.id" placeholder="ID"></el-input>
            </el-form-item> -->

      <el-row>
        <el-col :span="12">
          <el-form-item label="地区名称" prop="name">
            <el-input v-model="dataForm.name" placeholder="地区名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="温度范围" prop="temperature">
            <el-input
              v-model="dataForm.temperature"
              placeholder="温度范围"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="气象时间" prop="weatherDate">
            <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="dataForm.weatherDate"
              type="datetime"
              placeholder="气象时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="经度" prop="longitude">
            <el-input
              v-model="dataForm.longitude"
              placeholder="经度"
              type="number"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="纬度" prop="latitude">
            <el-input
              v-model="dataForm.latitude"
              placeholder="纬度"
              type="number"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="空气质量 " prop="airQuality">
            <el-input
              v-model="dataForm.airQuality"
              placeholder="空气质量 "
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="空气质量指数 " prop="airQualityIndex">
            <el-input
              v-model="dataForm.airQualityIndex"
              placeholder="空气质量指数 "
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="天气类型" prop="type">
            <SelectComponent
              ref="type"
              dictType="weather_type"
              v-model="dataForm.type"
              placeholder="天气类型"
            >
            </SelectComponent>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="风力 " prop="windPower">
            <el-input
              v-model="dataForm.windPower"
              placeholder="风力 "
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="风向 " prop="windDirection">
            <el-input
              v-model="dataForm.windDirection"
              placeholder="风向 "
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="日出时间" prop="sunUp">
            <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="dataForm.sunUp"
              type="datetime"
              placeholder="日出时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="日落时间" prop="sunDown">
            <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="dataForm.sunDown"
              type="datetime"
              placeholder="日落时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="月出时间" prop="moonUp">
            <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="dataForm.moonUp"
              type="datetime"
              placeholder="月出时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="月落时间" prop="moonDown">
            <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="dataForm.moonDown"
              type="datetime"
              placeholder="月落时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="月亮形状" prop="moonShape">
            <el-input
              v-model="dataForm.moonShape"
              placeholder="月亮形状（是否满月、残月等） "
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="省编码" prop="province">
            <el-select
              v-model="dataForm.province"
              placeholder="省编码"
              clearable
              @change="
                (val) => {
                  regionChange(val, 'province');
                }
              "
            >
              <el-option
                v-for="item in provinceOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="市" prop="city">
            <el-select
              v-model="dataForm.city"
              placeholder="市"
              clearable
              @change="
                (val) => {
                  regionChange(val, 'city');
                }
              "
            >
              <el-option
                v-for="item in cityOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="区" prop="area">
            <el-select
              v-model="dataForm.area"
              placeholder="区"
              clearable
              @change="
                (val) => {
                  regionChange(val, 'area');
                }
              "
            >
              <el-option
                v-for="item in areaOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="街道" prop="street">
            <el-input v-model="dataForm.street" placeholder="街道"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="详细地址" prop="areaDetail">
            <el-input
              v-model="dataForm.areaDetail"
              placeholder="详细地址"
              type="textarea"
              :rows="2"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="温馨提示" prop="tips">
            <el-input
              v-model="dataForm.tips"
              placeholder="温馨提示"
              type="textarea"
              :rows="2"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="详情介绍" prop="info">
            <el-input
              v-model="dataForm.info"
              placeholder="详情介绍"
              type="textarea"
              :rows="2"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="dataForm.remark"
              placeholder="备注"
              type="textarea"
              :rows="2"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"> </el-col>
      </el-row>
      <!-- <el-form-item label="'状态  0：停用    1：正常'" prop="status">
              <el-input v-model="dataForm.status" placeholder="'状态  0：停用    1：正常'"></el-input>
            </el-form-item> -->
      <!-- <el-form-item label="'删除标识  0：未删除    1：删除'" prop="delFlag">
              <el-input v-model="dataForm.delFlag" placeholder="'删除标识  0：未删除    1：删除'"></el-input>
            </el-form-item>
            <el-form-item label="创建者" prop="creator">
              <el-input v-model="dataForm.creator" placeholder="创建者"></el-input>
            </el-form-item>
            <el-form-item label="创建时间" prop="createDate">
              <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
            </el-form-item>
            <el-form-item label="更新者" prop="updater">
              <el-input v-model="dataForm.updater" placeholder="更新者"></el-input>
            </el-form-item>
            <el-form-item label="更新时间" prop="updateDate">
              <el-input v-model="dataForm.updateDate" placeholder="更新时间"></el-input>
            </el-form-item> -->
    </el-form>
    <template slot="footer" v-if="!disabled">
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
import mixinRegionModule from "@/mixins/region-module";
export default {
  mixins: [mixinRegionModule],
  data() {
    return {
      visible: false,
      dataForm: {
        id: "",
        name: "",
        temperature: "",
        weatherDate: "",
        longitude: "",
        latitude: "",
        airQuality: "",
        airQualityIndex: "",
        type: "",
        windPower: "",
        windDirection: "",
        sunUp: "",
        sunDown: "",
        moonUp: "",
        moonDown: "",
        moonShape: "",
        province: "",
        city: "",
        area: "",
        street: "",
        areaDetail: "",
        tips: "",
        info: "",
        status: "",
        remark: "",
        delFlag: "",
        creator: "",
        createDate: "",
        updater: "",
        updateDate: "",
      },
    };
  },
  props: { disabled: Boolean },

  created() {
    this.provincAreaDetailInfoList();
  },
  computed: {
    dataRule() {
      return {
        latitude: [
          {
            validator: this.$formValidate.validateDecimal("纬度", 11, 8),
            trigger: ["blur"],
          },
        ],
        longitude: [
          {
            validator: this.$formValidate.validateDecimal("经度", 11, 8),
            trigger: ["blur"],
          },
        ],
      };
    },
  },
  methods: {
    opened() {
      this.provincAreaDetailInfoList();
      if (this.dataForm.city) {
        this.cityAreaDetailInfoList(this.dataForm.province);
      }
      if (this.dataForm.area) {
        this.countyAreaDetailInfoList(this.dataForm.city);
      }
    },
    close() {
      this.dataForm = {
        id: "",
        name: "",
        temperature: "",
        weatherDate: "",
        longitude: "",
        latitude: "",
        airQuality: "",
        airQualityIndex: "",
        type: "",
        windPower: "",
        windDirection: "",
        sunUp: "",
        sunDown: "",
        moonUp: "",
        moonDown: "",
        moonShape: "",
        province: "",
        city: "",
        area: "",
        street: "",
        areaDetail: "",
        tips: "",
        info: "",
        status: "",
        remark: "",
        delFlag: "",
        creator: "",
        createDate: "",
        updater: "",
        updateDate: "",
      };
      this.$emit("changeDisabled");
    },

    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get("/sys/isweather/" + this.dataForm.id)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
          this.dataForm.type = this.dataForm.type + "";
          this.$refs.type.selectValue = this.dataForm.type;
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/sys/isweather/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
<style lang="scss" scoped >
/deep/ .el-input,
/deep/ .el-textarea {
  width: 80%;
}
</style>